<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增公寓信息')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-apartmentInfo-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在省份：</label>
                <div class="col-sm-8">
                    <select name="addressProvince" id="province" class="form-control" required>
                        <option value="">请选择省份</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在城市：</label>
                <div class="col-sm-8">
                    <select name="addressCity" id="city" class="form-control" required>
                        <option value="">请选择城市</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在区县：</label>
                <div class="col-sm-8">
                    <select name="addressNeighborhood" id="neighborhood" class="form-control" required>
                        <option value="">请选择区县</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓所在小区：</label>
                <div class="col-sm-8">
                    <input name="addressCommunity" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓单元：</label>
                <div class="col-sm-8">
                    <input name="unit" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓门牌号：</label>
                <div class="col-sm-8">
                    <input name="doorNumber" class="form-control" type="number" min="0101" max="9950" required
                           pattern="^(?:0[1-9]|[1-9]\d)(?:0[1-9]|[1-4]\d|50)$"
                           oninvalid="this.setCustomValidity('请输入正确的门牌号')"
                           oninput="this.setCustomValidity('')">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">公寓户型：</label>
                <div class="col-sm-8">
                    <select name="apartmentType" class="form-control" th:with="type=${@dict.getType('apartment_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">租金￥：</label>
                <div class="col-sm-8">
                    <input name="rent" class="form-control" type="number" min="0.01" step="0.01" required
                           oninvalid="this.setCustomValidity('请输入大于0的有效金额')"
                           oninput="this.setCustomValidity('')">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">公寓状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('apartment_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "apartment/apartmentInfo"
    $("#form-apartmentInfo-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-apartmentInfo-add').serialize());
        }
    }

    $(function() {
        const apiKey = '9f156c40ebe4bed6cb1f329f41e33b19'; // 替换为您的高德 API 密钥
        let provinceData = {};
        let cityData = {};

        // 获取省份
        $.get(`https://restapi.amap.com/v3/config/district?key=${apiKey}&subdistrict=1&extensions=base`, function(data) {
            if (data.status === '1') {
                data.districts[0].districts.forEach(function(province) {
                    provinceData[province.adcode] = province.name;
                    $('#province').append(`<option value="${province.adcode}">${province.name}</option>`);
                });
            } else {
                console.error('API返回数据失败', data.info);
            }
        });

        // 省份选择变化时获取城市
        $('#province').change(function() {
            const provinceCode = $(this).val();
            $('#city').empty().append('<option value="">请选择城市</option>');
            $('#neighborhood').empty().append('<option value="">请选择区/县</option>');

            if (provinceCode) {
                $.get(`https://restapi.amap.com/v3/config/district?key=${apiKey}&subdistrict=2&extensions=all&keywords=${provinceCode}`, function(data) {
                    if (data.status === '1') {
                        cityData = {};
                        data.districts[0].districts.forEach(function(city) {
                            cityData[city.adcode] = city.name;
                            $('#city').append(`<option value="${city.adcode}">${city.name}</option>`);
                        });
                    }
                });
            }
        });

        // 城市选择变化时获取区县
        $('#city').change(function() {
            const cityCode = $(this).val();
            $('#neighborhood').empty().append('<option value="">请选择区/县</option>');

            if (cityCode) {
                $.get(`https://restapi.amap.com/v3/config/district?key=${apiKey}&subdistrict=3&extensions=all&keywords=${cityCode}`, function(data) {
                    if (data.status === '1') {
                        data.districts[0].districts.forEach(function(district) {
                            $('#neighborhood').append(`<option value="${district.adcode}">${district.name}</option>`);
                        });
                    }
                });
            }
        });

        // 在表单提交前转换代码为名称
        $('#form-apartmentInfo-add').submit(function(e) {
            e.preventDefault();
            const provinceCode = $('#province').val();
            const cityCode = $('#city').val();
            const neighborhoodCode = $('#neighborhood').val();

            $('input[name="addressProvince"]').val(provinceData[provinceCode]);
            $('input[name="addressCity"]').val(cityData[cityCode]);
            $('input[name="addressNeighborhood"]').val($('#neighborhood option:selected').text());

            submitHandler();
        });
    });
</script>
</body>
</html>